﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EmgrMvc.BLL;
using EmgrMvc.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Quartz;

namespace EmgrMvc.Controllers
{
    public class TestController : Controller
    {
        private readonly MyContext _context;
        MqttTool tool;

        public TestController(MyContext context)
        {
            _context = context;
            tool = MqttTool.getSingleton(); // new MqttTool();
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Test()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Test(TestInfo info)
        {
            //MqttTool tool = new MqttTool(_context);
            if (info.Action == "ACPowerOn")
                tool.ACControl(info.Topic, info.Data);
            else if (info.Action == "ACPowerOff")
                tool.ACControl(info.Topic, info.Data);
            else if (info.Action == "WakeupPC")
                tool.WakeupPC(info.Topic, info.Data);
            else if (info.Action == "InitData")
            {
                EsysBLL bll = new EsysBLL(_context);
                bll.InitData();
                return Redirect("/DeviceGroups/Index");
            }

            return View();
        }

        public IActionResult Sql()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Sql([Bind("Text,Result")] SqlCommand model)
        {
            model.Result = _context.Database.ExecuteSqlRaw(model.Text);
            return View(model);
        }

        [HttpGet]
        public IActionResult Quartz()
        {
            return View();
        }

        [HttpPost]
        public string Quartz(string Action, string Topic, string Data, int DelayMinutes)
        {
            List<Device> Devices = new List<Device>();
            Devices.Add(_context.Devices.Find(5));
            //创建作业和触发器
            var jobDetail = JobBuilder.Create<IRControlJob>()
                .WithIdentity("id1", "group1")
                .SetJobData(new JobDataMap() {
                                new KeyValuePair<string, object>("Action", "ON"),
                                new KeyValuePair<string, object>("Devices", Devices),
                                new KeyValuePair<string, object>("Types", _context.Types.ToList()),
                                new KeyValuePair<string, object>("DbContext", _context)
                            }).Build();
            var trigger = TriggerBuilder.Create()
                                        //.WithCronSchedule("0 3 6 * * ?")
                                        //.WithSimpleSchedule(m => m.WithRepeatCount(3).WithIntervalInMinutes(1))
                                        .StartAt(DateTime.Now)
                                        .Build();

            //添加调度
            IScheduler scheduler = ScheduleTool.scheduler;
            scheduler.ScheduleJob(jobDetail, trigger);
            return "Hello";
        }

        public ActionResult ResetSchedule()
        {
            tool.ResetSchedule();
            return Redirect("Test");
        }

        public ActionResult JobTest()
        {
            OntimeJob job = _context.OntimeJobs.Find(2);
            DateTime jobTime = DateTime.Now + TimeSpan.FromSeconds(10);
            job.Hour = jobTime.Hour;
            job.Minute = jobTime.Minute;
            job.Second = jobTime.Second;

            job.DeviceOrGroup = "DEVICE";
            job.DeviceOrGroupId = 2;

            _context.SaveChanges();

            tool.ResetSchedule();

            return Redirect("Test");
        }

    }
}